﻿<%@ WebHandler Language="C#" Class="matching" %>

using System;
using System.Web;
using System.Text;
using System.Data;

public class matching : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "application/json";
        string id = context.Request.Params["id"];
        string drug_name = context.Request.Params["drug_name"];
        string batch_number = context.Request.Params["batch_number"];
        
        if (string.IsNullOrEmpty(drug_name) && string.IsNullOrEmpty(batch_number))
        {
            context.Response.Write("{\"code\":0,\"msg\":\"\u5339\u914d\u5931\u8d25\uff0c\u836f\u54c1\u540d\u79f0\u548c\u6279\u6b21\u53f7\u4e0d\u80fd\u4e3a\u7a7a\"}");
        }
        else
        {
            context.Response.Write(Data(id,drug_name,batch_number));
        }
    }

    MsSqlHelper mssql = new MsSqlHelper();
    
    public string Data(string id,string k,string batch)
    {
        string sql = "select top 48 rtrim(a.spid) as spid,rtrim(a.spbh) as spbh,rtrim(a.spmch) as spmch,rtrim(a.shpgg) as shpgg,rtrim(a.dw) as dw,rtrim(a.shengccj) as shengccj,rtrim(b.pihao) as pihao,rtrim(c.detailid) as detailid  "
                   + " from spkfk a(nolock),spls_rk b(nolock) left join [ImageDoc] c(nolock) on b.spid=c.detailid and b.pihao=c.fzxx1 "
                   +" where a.spid=b.spid and (spbh like '%" + k + "%' or spmch like '%" + k + "%' or zjm like '%" + k + "%' ) and b.pihao like '%" + batch + "%' group by a.spid,a.spbh,a.spmch,a.shpgg,a.dw,a.shengccj,b.pihao,c.detailid";
        DataSet ds=mssql.ExecuteDs(sql);
        
        string retsult="";
        retsult="{\"code\":1,\"html\":\"<table width=\\\"100%\\\" border=\\\"0\\\" cellspacing=\\\"0\\\" cellpadding=\\\"0\\\"  class=\\\"sebig\\\"><tr class=\\\"setit\\\"><td>药品名称</td><td>药品编号</td><td>批次号</td><td>规格</td><td >单位</td><td>生产厂家</td><td>检验报告</td><td>操作</td></tr>";
        foreach (DataRow row in ds.Tables[0].Rows)
        {

            retsult = retsult + "<tr class=\\\"setit\\\"><td>" + row["spmch"] + "</td><td>" + row["spbh"] + "</td><td>" + row["pihao"] + "</td><td>" + row["shpgg"] + "</td><td>" + row["dw"] + "</td><td>" + row["shengccj"] + "</td>";

            if (string.IsNullOrEmpty(row["detailid"].ToString()))
            {
                retsult = retsult + "<td>未上传</td>";
                retsult = retsult + "<td class=\\\"QiyeC\\\"><a class=\\\"save\\\" data-img=\\\"" + id + "\\\" data-batch=\\\"" + row["pihao"] + "\\\" data-id=\\\""+ row["spid"] +"\\\" data-way=\\\"cover\\\" href=\\\"javascript:;\\\">保存</a></td>";
            }
            else
            {
                retsult = retsult + "<td class=\\\"QiyeC QiyeA\\\">";
                sql = "select rtrim(imageid) as imageid,rtrim(url) as url from [ImageDoc](nolock) where detailid='" + row["detailid"].ToString() + "' and fzxx1='" + row["pihao"].ToString() + "' order by imageid";
                DataSet dss = mssql.ExecuteDs(sql);

                for (int i = 0; i < dss.Tables[0].Rows.Count; i++)
                {
                    if (i == 0)
                    {
                        retsult = retsult + "<a class=\\\"view\\\" href=\\\"" + dss.Tables[0].Rows[i]["url"].ToString() + "\\\" data-img=\\\"" + dss.Tables[0].Rows[i]["imageid"].ToString() + "\\\" rel=\\\"" + row["detailid"].ToString() + "\\\" class=\\\"view\\\">预览</a>";
                    }
                    else
                    {
                        retsult = retsult + "<a class=\\\"view\\\" href=\\\"" + dss.Tables[0].Rows[i]["url"].ToString() + "\\\" data-img=\\\"" + dss.Tables[0].Rows[i]["imageid"].ToString() + "\\\" rel=\\\"" + row["detailid"].ToString() + "\\\" class=\\\"view\\\" style=\\\"display:none;\\\">预览</a>";
                    }
                }
                retsult = retsult + "</td>";
                
                retsult = retsult + "<td class=\\\"QiyeC\\\">"
    + "<a style=\\\"margin-right:20px;\\\" class=\\\"save\\\" data-img=\\\"" + id + "\\\" data-id=\\\"" + row["spid"] + "\\\" data-way=\\\"cover\\\" href=\\\"javascript:;\\\">覆盖保存</a>"
    + "<a class=\\\"save\\\" data-img=\\\"" + id + "\\\" data-batch=\\\"" + row["pihao"].ToString() + "\\\" data-way=\\\"add\\\" data-id=\\\"" + row["spid"] + "\\\" href=\\\"javascript:;\\\">新增保存</a></td>";
            }
            retsult = retsult + "</tr>";
        }
       // retsult = retsult + "<tr class=\\\"setit\\\"><td>阿莫西林胶囊</td><td>49853</td><td>g170584</td><td>0.25g*50粒</td><td>盒</td><td>湖南科伦制药有限公司</td><td class=\\\"QiyeC QiyeA\\\"><a class=\\\"view\\\" href=\\\"http://51yywd.oss-cn-shanghai.aliyuncs.com/Uploads/a93/20171110/5a053a429ab1717442.jpg\\\"data-img=\\\"/Uploads/a93/20171110/5a053a429ab1717442.jpg\\\" rel=\\\"5a053a42280f42086c4a429a\\\" class=\\\"view\\\">预览</a></td><td class=\\\"QiyeC\\\"><a style=\\\"margin-right:20px;\\\" class=\\\"save\\\" data-img=\\\"5b9efda6280f4207c06dbac7\\\" data-id=\\\"5a053a42280f42086c4a429a\\\" data-way=\\\"cover\\\" href=\\\"javascript:;\\\">覆盖保存</a><a class=\\\"save\\\" data-img=\\\"5b9efda6280f4207c06dbac7\\\" data-way=\\\"add\\\" data-id=\\\"5a053a42280f42086c4a429a\\\" href=\\\"javascript:;\\\">新增保存</a></td></tr>";
        //retsult = retsult + "<!--" + sql + "-->";
        retsult = retsult + "</table>\"}";
        return retsult;
    } 
 
    public bool IsReusable {
        get {
            return false;
        }
    }

    /// <summary>
    /// <summary>
    /// 字符串转Unicode
    /// </summary>
    /// <param name="source">源字符串</param>
    /// <returns>Unicode编码后的字符串</returns>
    public static string String2Unicode(string source)
    {
        byte[] bytes = Encoding.Unicode.GetBytes(source);
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < bytes.Length; i += 2)
        {
            stringBuilder.AppendFormat("\\u{0}{1}", bytes[i + 1].ToString("x").PadLeft(2, '0'), bytes[i].ToString("x").PadLeft(2, '0'));
        }
        return stringBuilder.ToString();
    }

}